package com.example.a79273.shandianban.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.config.NetConfigYouRen;
import com.example.a79273.shandianban.model.CommentBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 79273 on 2016/9/1.
 */
public class CommentAdapter extends BaseAdapter {

    private List<CommentBean.DataBean.ListBean> mData;
    private LayoutInflater inflater;
    private Picasso picasso;

    public CommentAdapter(Context context, List<CommentBean.DataBean.ListBean> mData) {
        this.mData = mData;
        inflater = LayoutInflater.from(context);
        picasso = Picasso.with(context);
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public CommentBean.DataBean.ListBean getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommentAdapter.ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_comment, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        CommentBean.DataBean.ListBean bean = getItem(position);

        picasso.load(NetConfigYouRen.ROOT_URL + bean.getSrc()).into(holder.pic_iv);
        holder.name_tv.setText(bean.getName());
        holder.time_tv.setText(bean.getTime());
        holder.comment_tv.setText(bean.getComment());
        return convertView;
    }

    private static class ViewHolder {
        private ImageView pic_iv;
        private TextView name_tv, time_tv, comment_tv;

        public ViewHolder(View view) {
            pic_iv = (ImageView) view.findViewById(R.id.iv_commentItem_pic);
            name_tv = (TextView) view.findViewById(R.id.tv_commentItem_name);
            time_tv = (TextView) view.findViewById(R.id.tv_commentItem_time);
            comment_tv = (TextView) view.findViewById(R.id.tv_commentItem_comment);
        }
    }
}
